package com.zp.oj.judge.strategy;

import cn.hutool.json.JSONUtil;
import com.zp.oj.judge.model.ExecuteCodeRequest;
import com.zp.oj.judge.model.ExecuteCodeSandResponse;
import com.zp.oj.model.dto.question.JudgeCase;
import com.zp.oj.model.dto.question.JudgeConfig;
import com.zp.oj.judge.model.JudgeInfo;
import com.zp.oj.model.entity.Question;
import com.zp.oj.model.enums.JudgeInfoEnum;

import java.util.List;

public class DefaultJudgeStrategy implements JudgeStrategy{
    public static void main(String[] args) {
        String s = "2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97";
        DefaultJudgeStrategy defaultJudgeStrategy = new DefaultJudgeStrategy();
        s = defaultJudgeStrategy.dealOutPut(s);
        System.out.println(s);
    }

    public String dealOutPut(String s){
        // 清除回车
        s = s.replaceAll("\n", "");
        // 清除末尾回车
        int len = s.length();
        if(len != 0 && s.charAt(len - 1) == ' '){
            s = s.substring(0, len - 1);
        }
        return s;
    }
    public JudgeInfo doJudge(JudgeContent judgeContent, ExecuteCodeSandResponse executeCodeSandResponse){
        List<String> inputs = judgeContent.getInputs();
        List<String> outPuts = judgeContent.getOutPuts();
        Question question = judgeContent.getQuestion();
        List<JudgeCase> judgeCaseList = judgeContent.getJudgeCaseList();
        // 获取信息
        JudgeInfo judgeInfo = judgeContent.getJudgeInfo();
        JudgeInfoEnum judgeInfoEnum = JudgeInfoEnum.ACCEPT;

        Long time = judgeInfo.getTime() == null ? 0 : judgeInfo.getTime();
        Long memory = judgeInfo.getMemory() == null ? 0 : judgeInfo.getMemory();
        // 封装返回信息
        JudgeInfo judgeInfoResponse = new JudgeInfo();
        judgeInfoResponse.setMessage(judgeInfoEnum.getValue());
        judgeInfoResponse.setTime(time);
        judgeInfoResponse.setMemory(memory);
        String judgeConfigStr = question.getJudgeConfig();
        JudgeConfig judgeConfig = JSONUtil.toBean(judgeConfigStr, JudgeConfig.class);
        Long timeLimit = judgeConfig.getTimeLimit();
        Long memoryLimit = judgeConfig.getMemoryLimit();
        memory = memory / (1000 * 1000);
        // 查看是否編譯成功
        if(memory == 0 || time == 0){
            judgeInfoEnum = JudgeInfoEnum.COMPILE_ERROR;
            judgeInfoResponse.setMessage(judgeInfoEnum.getValue());
            return judgeInfoResponse;
        }
        if(memoryLimit <= memory){
            judgeInfoEnum = JudgeInfoEnum.MEMORY_LIMIT_EXCEEDED;
            judgeInfoResponse.setMessage(judgeInfoEnum.getValue());
            return judgeInfoResponse;
        }
        if(timeLimit < time){
            judgeInfoEnum = judgeInfoEnum.TIME_LIMIT_EXCEEDED;
            judgeInfoResponse.setMessage(judgeInfoEnum.getValue());
            return judgeInfoResponse;
        }
        // #根据返回结果判断是否正确
        if(outPuts.size() != inputs.size()){
            judgeInfoEnum = JudgeInfoEnum.WRONG_ANSWER;
            judgeInfoResponse.setMessage(judgeInfoEnum.getValue());
            return judgeInfoResponse;
        }
        for(int i = 0; i < judgeCaseList.size(); i++){
            JudgeCase judgeCase = judgeCaseList.get(i);
            String output = dealOutPut(outPuts.get(i)); // 得到被处理的字符串
            System.out.println("输出信息：" + output);
            if(!judgeCase.getOutput().equals(output)){
                judgeInfoEnum = JudgeInfoEnum.WRONG_ANSWER;
                judgeInfoResponse.setMessage(judgeInfoEnum.getValue());
                executeCodeSandResponse.setMessage("程序输出:" + output + "\n" + "正确输出:" + judgeCase.getOutput());
                return judgeInfoResponse;
            }
        }
        // 判读判题配置;
        return  judgeInfoResponse;
    }
}
